﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CprUserControl
{
    /// <summary>
    /// Interaction logic for ucCPR.xaml
    /// </summary>
    public partial class ucCPR : UserControl, INotifyPropertyChanged
    {
        enum CprState
        {
            NoError,
            CheckFormatError,
            Check11Error,
            CheckDateError
        }

        public ucCPR()
        {
            InitializeComponent();
            
        }

        private string _cprLabel;
        private string _cprNumber;

        public string CprLabel
        {
            get { return _cprLabel; }
            set
            {
                _cprLabel = value;
                OnPropertyChanged("CprLabel");
            }
        }
        public string CprNumber
        {
            get { return _cprNumber; }
            set {
                if (CheckFormat(value) == CprState.NoError && Check11(value) == CprState.NoError)
                {
                    _cprNumber = value;
                    txtCpr.Background = Brushes.Green;
                    
                }
                else
                {

                    txtCpr.Background = Brushes.Red;
                }
                OnPropertyChanged("CprNumber");
                
            } }

        
        private CprState CheckFormat(string txt)
        {
            CprState cprError = CprState.NoError;
            if (!(txt.Length == 10))
            {
                cprError = CprState.CheckFormatError;
            }

            return cprError;
        }

        private CprState CheckDate(string txt)
        {
            CprState cprError = CprState.NoError;
            string txtDate = txt.Substring(0, 6);
            DateTime date;

            try
            {
                date = DateTime.ParseExact(txtDate, "ddMMyy",CultureInfo.InvariantCulture);
            }
            catch (FormatException)
            {
                cprError = CprState.Check11Error;
            }

            //if date


            return cprError;
        }

        private CprState Check11(string txt)
        {
            CprState cprError = CprState.Check11Error;

            if (txt.Length == 10)
            {
                string txt11 = txt.Substring(6, 4);
                if((int.Parse(txt11) % 11) == 0)
                {
                    cprError = CprState.NoError;
                }

            }
            return cprError;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
